﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.AppCode.Lib;

namespace mojoPortal.Web.RS.Modules.RFQ
{
    public partial class RSRFQConfirm : RSBasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string rfqid = "";
                string rfqtoken = "";
                if (Request.QueryString["p1"] != null)
                {
                    rfqid = Request.QueryString["p1"].ToString();
                    if (!RSCommonFunctions.IsNumeric(rfqid))
                        rfqid = "0";
                }
                if (Request.QueryString["p2"] != null)
                {
                    rfqtoken = Request.QueryString["p2"].ToString();
                }

                hfRFQID.Value = rfqid;
                hfRFQToken.Value = rfqtoken;
            }
        }
        public void btnSubmitUserEmail_Click(object sender, EventArgs e)
        {
            int rfqid = hfRFQID.Value == "" ? 0 : int.Parse(hfRFQID.Value);
            string rfqtoken = hfRFQToken.Value;
            string useremail = txtUserEmail.Text;
            if (!LoadRFQData(rfqid, rfqtoken, useremail))
            {
                lblMessage.Text = "Sorry.  Your user email and RFQ entry could not be confirmed.";
                lblMessage.ForeColor = System.Drawing.Color.DarkRed;
                pnlRFQDetail.Visible = false;
                pnlUserEmail.Visible = true;
            }
            else
            {
                lblMessage.Text = "Please confirm your RFQ information, save any changes and select Submit.";
                lblMessage.ForeColor = System.Drawing.Color.Black;
                pnlUserEmail.Visible = false;
                pnlRFQDetail.Visible = true;
            }
        }
        public bool LoadRFQData(int rfqid, string rfqtoken, string useremail)
        {
            bool ret = false;
            hfRFQID.Value = rfqid.ToString();
            hfRFQToken.Value = rfqtoken;
            if (rfqid > 0 && rfqtoken.Length > 0 && useremail.Length > 0)
            {
                RSDataLookupLoader.LoadPublicTypeDDL("INDUSTRY_CAT", ddlIndustryType);
                RSDataLookupLoader.LoadPublicStatusDDL("RFQ_CAT", ddlRFQStatus);

                String sql = "EXEC rs_uspGetPublicRFQ @RFQID, @RFQToken, @UserEmail";
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ReadyServiceConnectionStringMain"].ToString());
                myCommand = new SqlCommand(sql, myConnection);
                SqlDataReader myDataReader;

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@RFQID";
                param.Value = rfqid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.NVarChar;
                param.ParameterName = "@RFQToken";
                param.Value = rfqtoken;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.NVarChar;
                param.ParameterName = "@UserEmail";
                param.Value = useremail;
                myCommand.Parameters.Add(param);

                myConnection.Open();
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (myDataReader.HasRows)
                {
                    myDataReader.Read();
                    txtEmail.Text = myDataReader["ContactEmail"].ToString();
                    txtContactName.Text = myDataReader["ContactName"].ToString();
                    txtMobile.Text = myDataReader["ContactMobile"].ToString();
                    txtOffice.Text = myDataReader["ContactOffice"].ToString();
                    txtSubject.Text = myDataReader["Subject"].ToString();
                    txtAdditionalInstruction.Text = myDataReader["AdditionalInstruction"].ToString();
                    txtTerm.Text = myDataReader["Term"].ToString();
                    txtDescription.Text = myDataReader["Description"].ToString();
                    if (myDataReader["BidCloseDt"] != DBNull.Value)
                    {
                        DateTime bidclosedt = Convert.ToDateTime(myDataReader["BidCloseDt"].ToString());
                        txtCloseDate.Text = bidclosedt.ToShortDateString();
                    }
                    if (myDataReader["Status"] != DBNull.Value)
                    {
                        if (int.Parse(myDataReader["Status"].ToString()) > 0 ) 
                            ddlRFQStatus.SelectedValue = myDataReader["Status"].ToString();
                    }
                    if (myDataReader["IndustryType"] != DBNull.Value)
                    {
                        ddlIndustryType.SelectedValue = myDataReader["IndustryType"].ToString();
                    }
                    if (myDataReader["IsApproved"] != DBNull.Value)
                    {
                        hfIsApproved.Value = myDataReader["IsApproved"].ToString();
                    }

                    ret = true;
                }
                myConnection.Close();
            }
            return ret;
        }
        
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int rfqid = int.Parse(hfRFQID.Value);
            string rfqtoken = hfRFQToken.Value;
            int IndustryType = int.Parse(ddlIndustryType.SelectedValue);
            int status = int.Parse(ddlRFQStatus.SelectedValue);
            string CloseDate = txtCloseDate.Text;
            string ContactName = txtContactName.Text;
            string ContactMobile = txtMobile.Text;
            string ContactOffice = txtOffice.Text;
            string ContactEmail = txtEmail.Text;
            string Subject = txtSubject.Text;
            string AdditionalInstruction = txtAdditionalInstruction.Text;
            string Term = txtTerm.Text;
            string Description = txtDescription.Text;
            bool isapproved = hfIsApproved.Value == "" ? false : bool.Parse(hfIsApproved.Value);
            bool isconfirmed = true;

            SqlCommand sqlCmd = new SqlCommand();
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@IndustryType", SqlDbType.Int, 0, ParameterDirection.Input, IndustryType);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContactName", SqlDbType.NVarChar, 50, ParameterDirection.Input, ContactName);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@BidCloseDt", SqlDbType.DateTime, 0, ParameterDirection.Input, Convert.ToDateTime(CloseDate));
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContactMobile", SqlDbType.NVarChar, 15, ParameterDirection.Input, ContactMobile);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContactOffice", SqlDbType.NVarChar, 15, ParameterDirection.Input, ContactOffice);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContactEmail", SqlDbType.NVarChar, 50, ParameterDirection.Input, ContactEmail);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Subject", SqlDbType.NVarChar, 250, ParameterDirection.Input, Subject);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AdditionalInstruction", SqlDbType.NVarChar, -1, ParameterDirection.Input, AdditionalInstruction);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Term", SqlDbType.NVarChar, -1, ParameterDirection.Input, Term);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Description", SqlDbType.NVarChar, -1, ParameterDirection.Input, Description);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@RFQToken", SqlDbType.NVarChar, 50, ParameterDirection.Input, rfqtoken);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@RFQID", SqlDbType.Int, 0, ParameterDirection.Input, rfqid);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Status", SqlDbType.Int, 0, ParameterDirection.Input, status);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@IsConfirmed", SqlDbType.Bit, 0, ParameterDirection.Input, isconfirmed);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@IsApproved", SqlDbType.Bit, 0, ParameterDirection.Input, isapproved);
            SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSavePublicRFQ");
            SQLHelper.ExecuteScalarCmd_ReadyServiceMain(sqlCmd);

            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            lblMessage.Text = "RFQ Successfully Saved.";
            lblMessage.ForeColor = System.Drawing.Color.DarkBlue;
            btnSave.Visible = false;
            btnDelete.Visible = false;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {

        }
    }
}

